package com.pybox.api;

import com.pybox.aop.Mylog;
import com.pybox.dto.*;
import com.pybox.service.TObjectsCabinetService;
import com.pybox.service.TObjectsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.Cache;
import org.springframework.cache.CacheManager;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：TObjects Api
 *
 * @author wuyingkai
 * @date 2018-09-15
 */

@Slf4j
@RequestMapping("/TObjects")
@Api(tags = "TObjects管理接口", description = "TObjects管理接口")
@RestController
public class TObjectsApi {

    @Autowired
    private TObjectsService service;

    @Autowired
    private TObjectsCabinetService objectService;

    @Autowired
    private CacheManager cacheManager;

    @ApiOperation(value = "创建TObjects", notes = "创建TObjects")
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @Mylog(value = "创建了物品信息")
    public Result<Boolean> save(@RequestBody TObjectsDto dto) {
        service.create(dto);
        return Result.ok(true);
    }

    @ApiOperation(value = "删除TObjects", notes = "删除TObjects")
    @Mylog(value = "删除了物品信息")
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.DELETE)
    @ApiImplicitParam(name = "id", value = "TObjects id", dataType = "String", paramType = "path")
    public Result<Boolean> delete(@PathVariable("id") Integer id) {
        service.delete(id);
        return Result.ok(true);
    }

    @ApiOperation(value = "更新TObjects", notes = "更新TObjects")
    @RequestMapping(value = "/update/{id}", method = RequestMethod.PUT)
    @Mylog(value = "更新了物品信息")
    @ApiImplicitParam(name = "id", value = "TObjects id", dataType = "String", paramType = "path")
    public Result<Boolean> update(@PathVariable("id") Integer id, @RequestBody TObjectsDto dto) {
        service.update(id, dto);
        return Result.ok(true);
    }

    @ApiOperation(value = "查询TObjects", notes = "查询TObjects")
    @RequestMapping(value = "/detail/{id}", method = RequestMethod.GET)
    @ApiImplicitParam(name = "id", value = "TObjects id", dataType = "String", paramType = "path")
    public Result<TObjectsDto> detail(@PathVariable("id") Integer id) {

        return Result.ok(service.detail(id));
    }


    /*根据客户Id查询所有物品*/
    @ApiOperation(value = "根据客户Id查询所有物品", notes = "根据客户Id查询所有物品")
    @RequestMapping(value = "/searchAll", method = RequestMethod.GET)
    @ResponseBody
    public Result<List<TObjectsDto>> searchAll() {
        Cache cache = cacheManager.getCache("getObjectsCache");
        Cache.ValueWrapper valueWrapper = cache.get("getObjectsCache.page");
        List<TObjectsDto> list;
//        if (valueWrapper != null) {
//            list = (List<TObjectsDto>) valueWrapper.get();
//            //log.info(list.size()+"+_+");
//
//        } else {
            list = service.searchAll();
            //log.info(list.size()+"+_+4444");
       // }
        return Result.ok(list);
    }


    @ApiOperation(value = "分页查询TObjects", notes = "分页查询TObjects")
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "id", dataType = "Integer", paramType = "query"),
            @ApiImplicitParam(name = "name", value = "name", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "customerId", value = "customerId", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "category", value = "category", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "color", value = "color", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "day", value = "day", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "status", value = "status", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "hour", value = "hour", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "minute", value = "minute", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "comments", value = "comments", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "deleted", value = "deleted", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "ctime", value = "ctime", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "uuid", value = "uuid", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "utime", value = "utime", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageNo", value = "pageNo", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "pageSize", dataType = "int", paramType = "query")
    })
    public Result<Page<TObjectsDto>> page(
            @RequestParam(required = false) Integer id,
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String customerId,
            @RequestParam(required = false) String category,
            @RequestParam(required = false) String color,
            @RequestParam(required = false) String day,
            @RequestParam(required = false) String hour,
            @RequestParam(required = false) String minute,
            @RequestParam(required = false) String comments,
            @RequestParam(required = false) String deleted,
            @RequestParam(required = false) String ctime,
            @RequestParam(required = false) String status,
            @RequestParam(required = false) String utime,
            @RequestParam(required = false) String isRelated,
            @RequestParam(required = false) String uuid,
            @RequestParam(required = true) Integer pageNo,
            @RequestParam(required = true) Integer pageSize) {

        Map<String, String> params = new HashMap<>();

        params.put("name", name);
        params.put("customerId", customerId);
        params.put("category", category);
        params.put("color", color);
        params.put("day", day);
        params.put("hour", hour);
        params.put("minute", minute);
        params.put("comments", comments);
        params.put("deleted", deleted);
        params.put("ctime", ctime);
        params.put("utime", utime);
        params.put("status", status);  //物品状态
        params.put("uuid", uuid);
        params.put("isRelated", isRelated);//是否关联分组人员

        //Cache cache = cacheManager.getCache("getAllObjectsCache");
        // Cache.ValueWrapper valueWrapper = cache.get("getAllObjectsCache.page");
       // Page<TObjectsDto> page = null;
        //if(valueWrapper != null) {
        //page = (Page<TObjectsDto>)valueWrapper.get();
        // log.info(page.getTotalPages()+"+_+");

        //}else{
        //page = service.page(pageNo, pageSize, params);
        // log.info(page.getTotalPages()+"+_+4444");
        // }

        return Result.ok(service.page(pageNo, pageSize, params));
    }

    @ApiOperation(value = "设置物品关联的分组", notes = "设置物品关联的分组")
    @RequestMapping(value = "/updateGroupsByObjectId", method = RequestMethod.PUT)
    public Result<Boolean> updateGroupsByObjectId(@RequestBody ObjectGroupsDto dto) {
        return service.updateGroupsByObjectId(dto);

    }

    @ApiOperation(value = "设置物品关联的用户", notes = "设置物品关联的用户")
    @RequestMapping(value = "/updateObjectsByUserId", method = RequestMethod.PUT)
    public Result<Boolean> updateObjectsByUserId(@RequestBody ObjectUsersDto dto) {
        return service.updateUsersByObjectId(dto);
    }

    @ApiOperation(value = "获取物品关联的用户", notes = "获取物品关联的用户")
    @RequestMapping(value = "/getUsersByObjectId/{ObjectId}", method = RequestMethod.GET)
    public Result<ObjectUsersDto> getUsersByObjectId(@PathVariable Integer ObjectId) {
        return service.getUserIdsByObjectId(ObjectId);
    }

    @ApiOperation(value = "获取物品关联的分组", notes = "获取物品关联的分组")
    @RequestMapping(value = "/getGroupsByObjectId/{objectId}", method = RequestMethod.GET)
    public Result<ObjectGroupsDto> getGroupsByObjectId(@PathVariable Integer objectId) {
        return service.getGroupIdsByObjectId(objectId);
    }

    @ApiOperation(value = "获取未和分组关联的物品", notes = "获取分组关联的物品")
    @RequestMapping(value = "/getObjectsNoGroupId", method = RequestMethod.GET)
    public Result<GroupObjectsDto> getObjectsNoGroupId() {
        return service.getObjectsNoGroupId();
    }



    @ApiOperation(value = "根据用户Id分页查询TObjects", notes = "根据用户Id分页查询TObjects")
    @RequestMapping(value = "/searchObjectByUserId", method = RequestMethod.GET)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "name", dataType = "string", paramType = "query"),
            @ApiImplicitParam(name = "pageNo", value = "pageNo", dataType = "int", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "pageSize", dataType = "int", paramType = "query")
    })
    public Result<Page<TObjectsDto>> searchObjectByUserId(
            @RequestParam(required = false) String name,
            @RequestParam(required = false) String cabinetName,
            @RequestParam Integer pageNo,
            @RequestParam Integer pageSize) {
        Map<String, String> params = new HashMap<>();
        params.put("name", name);
        params.put("cabinetName", cabinetName);
        return Result.ok(service.searchObjectByUserId(pageNo, pageSize, params));
    }


}